package com.wxy.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2022/10/31 11:15
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件管理")
@Slf4j
public class FileController {

    @PutMapping("/upload")
    @ApiOperation("上传文件")
    @ApiImplicitParam(name = "token", paramType = "header")
    public String upload(MultipartFile file, @RequestPart String name, String token) {
        return name + "===" + file.getOriginalFilename() + "===" + token;
    }

    @GetMapping("/download")
    @ApiOperation("下载文件")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;filename=download.txt");
        try (ServletOutputStream out = response.getOutputStream()) {
            out.print("恭喜你，下载成功！");
        }
    }
}
